package cn.zust.ly.controller;


import cn.zust.ly.dto.PicDto;
import cn.zust.ly.dto.SessionInfo;
import cn.zust.ly.service.PictureService;
import cn.zust.ly.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by liy on 2016/11/9.
 */
@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private PictureService pictureService;

    @RequestMapping("doLogin")
    @ResponseBody
    public boolean doLogin(
            HttpSession session,
            @RequestParam("username") String username,
            @RequestParam("password") String password) {
        session.setAttribute("tuser", userService.getUserByLoginName(username));
        return userService.doLogin(username, password, session);
    }

    @RequestMapping("index")
    public ModelAndView index(HttpServletRequest request, HttpSession session) {
        ModelAndView mv = new ModelAndView("index");
        int userId = ((SessionInfo) session.getAttribute("sessionInfo")).getUserId();
        PicDto userOnlyPic = pictureService.getUserOnlyPic(userId);
        List<PicDto> picDtos = pictureService.getUserPics(userId);
        List<PicDto> concernerPic = pictureService.getUserConcernerPics(userId);
        mv.addObject("myonlyPic", userOnlyPic);
        mv.addObject("myPics", picDtos);
        mv.addObject("concernerPic", concernerPic);

        return mv;
    }




    @RequestMapping("doLogout")
    public String daLogout(HttpSession session) {
        session.removeAttribute("tuser");
        return "redirect:login.html";
    }


    @RequestMapping(value = "newPic",  method = RequestMethod.POST)
    @ResponseBody
    public boolean createPic (
            HttpSession session, HttpServletRequest request,
            @RequestParam(value = "pic", required = false) String pic,
            @RequestParam(value = "pic2", required = false) String pic2,
            @RequestParam(value = "pic3", required = false) String pic3,
            @RequestParam(value = "tags", required = false) String tags
            ) {
        int userId = ((SessionInfo) session.getAttribute("sessionInfo")).getUserId();
        System.out.println(userId+"--"+pic+"--"+pic2+"--"+pic3+"--"+tags);
        return pictureService.createPics( new String[]{pic, pic2, pic3}, tags, userId);
    }


//    @RequestMapping(value = "doRegister", method = RequestMethod.POST)
//    @ResponseBody
//    public boolean doRegister(Tuser tuser) {
//        return userService.doRegister(tuser);
//    }

//    @RequestMapping("userIndex")
//    public ModelAndView userIndex(HttpSession session) {
//        ModelAndView mv = new ModelAndView("userIndex");
//        Tuser user = (Tuser) session.getAttribute("tuser");
//        List<EquipmentDto> tequipments = userService.getOneUserAllTequipment(user.getId());
//        mv.addObject("user", user);
//        mv.addObject("equipments", tequipments);
//        return mv;
//    }

//    @RequestMapping("adminIndex")
//    public ModelAndView adminIndex(HttpSession session) {
//        ModelAndView mv = new ModelAndView("adminIndex");
//        Tuser user = (Tuser) session.getAttribute("tuser");
//        List<Tuser> users = userService.getAllUser();
//        List<EquipmentDto> tequipments = userService.getAllTequipment();
//        mv.addObject("user", user);
//        mv.addObject("users", users);
//        mv.addObject("equipments", tequipments);
//        return mv;
//    }

//    @RequestMapping(value = "addEquipment", method = RequestMethod.POST)
//    @ResponseBody
//    public boolean addEquipment(EquipmentDto equipmentDto) {
//        System.out.println("in the addController");
//        return userService.addEquipment(equipmentDto);
//    }

}
